/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.templates.client.widgets;

import java.util.Iterator;
import java.util.NoSuchElementException;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * A simple panel that allow replacing its elements
 * 
 * Must be kept in sync with {@link SimplePanel}
 * 
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class SimplePanelEx extends Panel implements HasReplaceElement {

    private Widget widget;

    /**
     * Creates an empty panel that uses a DIV for its contents.
     */
    public SimplePanelEx() {
        this(DOM.createDiv());
    }

    /**
     * Creates an empty panel that uses the specified browser element for its
     * contents.
     * 
     * @param elem the browser element to use
     */
    protected SimplePanelEx(Element elem) {
        setElement(elem);
    }

    /**
     * Adds a widget to this panel.
     * 
     * @param w the child widget to be added
     */
    @Override
    public void add(Widget w) {
        // Can't add() more than one widget to a SimplePanel.
        if (getWidget() != null) {
            throw new IllegalStateException(
                    "SimplePanel can only contain one child widget");
        }
        setWidget(w);
    }

    /**
     * Gets the panel's child widget.
     * 
     * @return the child widget, or <code>null</code> if none is present
     */
    public Widget getWidget() {
        return widget;
    }

    public Iterator<Widget> iterator() {
        // Return a simple iterator that enumerates the 0 or 1 elements in this
        // panel.
        return new Iterator<Widget>() {
            boolean hasElement = widget != null;

            Widget returned = null;

            public boolean hasNext() {
                return hasElement;
            }

            public Widget next() {
                if (!hasElement || (widget == null)) {
                    throw new NoSuchElementException();
                }
                hasElement = false;
                return (returned = widget);
            }

            public void remove() {
                if (returned != null) {
                    SimplePanelEx.this.remove(returned);
                }
            }
        };
    }

    @Override
    public boolean remove(Widget w) {
        // Validate.
        if (widget != w) {
            return false;
        }

        // Orphan.
        try {
            orphan(w);
        } finally {
            // Physical detach.
            getContainerElement().removeChild(w.getElement());

            // Logical detach.
            widget = null;
        }
        return true;
    }

    /**
     * Sets this panel's widget. Any existing child widget will be removed.
     * 
     * @param w the panel's new widget, or <code>null</code> to clear the panel
     */
    public void setWidget(Widget w) {
        // Validate
        if (w == widget) {
            return;
        }

        // Detach new child.
        if (w != null) {
            w.removeFromParent();
        }

        // Remove old child.
        if (widget != null) {
            remove(widget);
        }

        // Logical attach.
        widget = w;

        if (w != null) {
            // Physical attach.
            DOM.appendChild(getContainerElement(), widget.getElement());

            adopt(w);
        }
    }

    @Override
    public void addAndReplaceElement(Widget w,
            com.google.gwt.dom.client.Element toReplace) {
        // Validate
        if (w == widget) {
            return;
        }

        // Detach new child.
        if (w != null) {
            w.removeFromParent();
        }

        // Remove old child.
        if (widget != null) {
            remove(widget);
        }

        // Logical attach.
        widget = w;

        if (w != null) {
            // Physical attach.
            toReplace.getParentNode().replaceChild(widget.getElement(),
                    toReplace);

            adopt(w);
        }
    }

    /**
     * Override this method to specify that an element other than the root
     * element be the container for the panel's child widget. This can be useful
     * when you want to create a simple panel that decorates its contents.
     * 
     * Note that this method continues to return the
     * {@link com.google.gwt.user.client.Element} class defined in the
     * <code>User</code> module to maintain backwards compatibility.
     * 
     * @return the element to be used as the panel's container
     */
    protected com.google.gwt.user.client.Element getContainerElement() {
        return getElement();
    }

}
